package com.itouchtv.yang.core.eventListener;

import com.itouchtv.yang.core.Enum.ResultEnum;
import com.itouchtv.yang.core.driver.CommDriver;
import com.itouchtv.yang.core.tools.Log;
import com.itouchtv.yang.core.tools.Screen;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;

import java.io.IOException;

public class MyTestListener implements ITestListener   {
    @Override
    public void onTestStart(ITestResult result) {
        Log.logger.info("开始测试:" + result.getTestClass().getName() + "." + result.getMethod().getMethodName());
    }

    @Override
    public void onTestSuccess(ITestResult result) {
        Log.logger.info("测试通过:" + result.getTestClass().getName() + "." + result.getMethod().getMethodName());
        Log.logger.info("用时："+(result.getEndMillis()-result.getStartMillis())+"ms");
        screen(result,ResultEnum.PASS);
    }

    @Override
    public void onTestFailure(ITestResult result) {
        Log.logger.info("测试失败:" + result.getTestClass().getName() + "." + result.getMethod().getMethodName());
        Log.logger.info("用时："+(result.getEndMillis()-result.getStartMillis())+"ms");
        screen(result,ResultEnum.FIAL);
    }

    @Override
    public void onTestSkipped(ITestResult result) {
        Log.logger.info("测试跳过:" + result.getTestClass().getName() + "." + result.getMethod().getMethodName());
        Log.logger.info("用时："+(result.getEndMillis()-result.getStartMillis())+"ms");
        screen(result,ResultEnum.SKIP);
    }

    @Override
    public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
    }

    @Override
    public void onStart(ITestContext context) {
    }

    @Override
    public void onFinish(ITestContext context) {
    }

    /**
     * 截图关联ExtentReport
     */
    private void screen(ITestResult result, ResultEnum resultEnum){
        try {
            ExtentReportListener.getInstance()
                    .addScreenCaptureFromPath(result, Screen.ScreenShot(CommDriver.driver.get(), resultEnum));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
